package config

import (
	"fmt"
	"github.com/spf13/viper"
	"time"
)

var (
	c Config
)

func GetConfig() *Config {
	return &c
}

type Config struct {
	MySQL  MySQLConfig
	App    AppConfig
	Server ServerConfig
}

type MySQLConfig struct {
	Host        string
	Name        string
	Password    string
	Port        int
	TablePrefix string
	User        string
}

type AppConfig struct {
	DefaultPageSize       int
	MaxPageSize           int
	DefaultContextTimeout time.Duration
	LogSavePath           string
	LogFileName           string
	LogFileExt            string
	ImageDir              string
}

type ServerConfig struct {
	RunMode      string
	HttpPort     string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

func init() {
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(".")
	viper.AutomaticEnv()

	err := viper.ReadInConfig()

	if err != nil {
		panic(fmt.Errorf("viper.ReadInConfig err: %s", err))
	}

	err = viper.Unmarshal(&c)
	if err != nil {
		panic(fmt.Errorf("viper.Unmarshal err: %s", err))
	}
}
